package com.che.libcommon.recycler;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EdgeEffect;

import com.che.common.libcommon.R;

import com.che.libcommon.recycler.DividerItemDecoration.DrawRules;
import java.util.ArrayList;
import java.util.List;

public class RecyclerView extends android.support.v7.widget.RecyclerView {



    EdgeEffect topGlow;
    EdgeEffect bottomGlow;


    private boolean drag = true;
    long prevScroll = 0;
    private boolean childDrawingOrderCallbackSet = false;

    private int scrollX = 0;
    private int scrollY = 0;

    public RecyclerView(Context context) {
        super(context, null, 0);
        initRecycler(null, 0);
    }

    public RecyclerView(Context context, AttributeSet attrs) {
        super(context,attrs);
        initRecycler(attrs, 0);
    }

    public RecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context,attrs,defStyleAttr);
        initRecycler(attrs, defStyleAttr);
    }



    private void initRecycler(AttributeSet attrs, int defStyleAttr) {
//        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RecyclerView, defStyleAttr, R.style.carbon_RecyclerView);

//        for (int i = 0; i < a.getIndexCount(); i++) {
//            int attr = a.getIndex(i);
//            if (attr == R.styleable.RecyclerView_carbon_overScroll) {
//                setOverScrollMode(a.getInt(attr, OVER_SCROLL_ALWAYS));
//            } else if (attr == R.styleable.RecyclerView_android_divider) {
//                Drawable drawable = a.getDrawable(attr);
//                float height = a.getDimension(R.styleable.RecyclerView_android_dividerHeight, 0);
//                if (drawable != null && height > 0) {
//                    setDivider(drawable, (int) height);
//                }
//            } else if (attr == R.styleable.RecyclerView_edgeEffectOffsetTop) {
//                setEdgeEffectOffsetTop(a.getDimension(attr, 0));
//            } else if (attr == R.styleable.RecyclerView_edgeEffectOffsetBottom) {
//                setEdgeEffectOffsetBottom(a.getDimension(attr, 0));
//            }
//        }
//
//        a.recycle();

        setClipToPadding(false);
        setWillNotDraw(false);
    }

    public void setDivider(Drawable divider, int height) {
        DividerItemDecoration decoration = new DividerItemDecoration(divider, height);
        decoration.setDrawBefore(new DrawRules() {
            @Override
            public boolean draw(int position) {
                return position > 0;
            }
        });
        addItemDecoration(decoration);
    }

    @Override
    public void offsetChildrenHorizontal(int dx) {
        super.offsetChildrenHorizontal(dx);
        scrollX -= dx;
    }

    public int getListScrollX() {
        return scrollX;
    }

    public int getMaxScrollX() {
        Adapter adapter = getAdapter();
        LayoutManager layoutManager = getLayoutManager();
        if (adapter == null || layoutManager == null || getChildCount() == 0 || adapter.getItemCount() == 0)
            return 0;
        View view = getChildAt(getChildCount() - 1);
        int position = getChildAdapterPosition(view);
        if (position == adapter.getItemCount())
            return Math.max(0, view.getRight() - getWidth());
        return Integer.MAX_VALUE;
    }

    @Override
    public void offsetChildrenVertical(int dy) {
        super.offsetChildrenVertical(dy);
        scrollY -= dy;
    }

    public int getListScrollY() {
        return scrollY;
    }

    public int getMaxScrollY() {
        Adapter adapter = getAdapter();
        LayoutManager layoutManager = getLayoutManager();
        if (adapter == null || layoutManager == null || getChildCount() == 0 || adapter.getItemCount() == 0)
            return 0;
        View view = getChildAt(getChildCount() - 1);
        int position = getChildAdapterPosition(view);
        if (position == adapter.getItemCount())
            return Math.max(0, view.getBottom() - getHeight());
        return Integer.MAX_VALUE;
    }

    public static abstract class Pagination extends OnScrollListener {
        private LinearLayoutManager layoutManager;

        public Pagination(LinearLayoutManager layoutManager) {
            this.layoutManager = layoutManager;
        }

        @Override
        public void onScrolled(android.support.v7.widget.RecyclerView recyclerView, int dx, int dy) {
            int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();

            if (!isLoading() && !isLastPage()) {
                if (layoutManager.getChildCount() + firstVisibleItemPosition >= layoutManager.getItemCount())
                    loadNextPage();
            }
        }

        protected abstract boolean isLoading();

        protected abstract boolean isLastPage();

        protected abstract void loadNextPage();
    }

    Pagination pagination;

    public void setPagination(Pagination pagination) {
        if (this.pagination != null)
            removeOnScrollListener(this.pagination);
        this.pagination = pagination;
        if (pagination != null)
            addOnScrollListener(pagination);
    }



    List<View> views = new ArrayList<View>();

    public List<View> getViews() {
        views.clear();
        for (int i = 0; i < getChildCount(); i++)
            views.add(getChildAt(i));
        return views;
    }

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);







    @Override
    protected int getChildDrawingOrder(int childCount, int child) {
        if (childDrawingOrderCallbackSet)
            return super.getChildDrawingOrder(childCount, child);
        if (views.size() != childCount)
            getViews();
        return indexOfChild(views.get(child));
    }

    @Override
    public void setChildDrawingOrderCallback(ChildDrawingOrderCallback childDrawingOrderCallback) {
        super.setChildDrawingOrderCallback(childDrawingOrderCallback);
        childDrawingOrderCallbackSet = childDrawingOrderCallback != null;
    }
    public void setHeight(int height) {
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        if (layoutParams == null) {
            setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height));
        } else {
            layoutParams.height = height;
            setLayoutParams(layoutParams);
        }
    }

    public void setSize(int width, int height) {
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        if (layoutParams == null) {
            setLayoutParams(new ViewGroup.LayoutParams(width, height));
        } else {
            layoutParams.width = width;
            layoutParams.height = height;
            setLayoutParams(layoutParams);
        }
    }
}
